package com.sbtr.business.calibration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.open.calibration.dto.CalibrationRecordDailyDto;
import com.sbtr.business.calibration.entity.CalibrationRecordDaily;
import com.sbtr.business.calibration.mapper.CalibrationRecordDailyMapper;
import com.sbtr.business.calibration.service.ICalibrationRecordDailyService;
import com.sbtr.constant.ColumnConstant;
import com.sbtr.util.BeanCopyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Transactional
@Service
public class CalibrationRecordDailyServiceImpl extends ServiceImpl<CalibrationRecordDailyMapper, CalibrationRecordDaily>
        implements ICalibrationRecordDailyService {
    private static final Logger logger = LoggerFactory.getLogger(CalibrationRecordDailyServiceImpl.class);

    @Override
    public CalibrationRecordDailyDto findById(Long id){
        return BeanCopyUtils.copyProperties(getById(id), CalibrationRecordDailyDto.class);
    }

    //查找当天的标记统计记录
    @Override
    public CalibrationRecordDaily findByCalibrationIdFinisher(Long calibrationId, String finisher){
        QueryWrapper<CalibrationRecordDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("calibration_id", calibrationId );
        wrapper.eq("finisher", finisher);
        Calendar calendar = new GregorianCalendar();
//        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//当天时间赋给日历对象
        calendar.set(Calendar.HOUR_OF_DAY, 0);//设置对象日历格式
        calendar.set(Calendar.MINUTE, 0);//设置为当天零点
        calendar.set(Calendar.SECOND, 0);
        Date updateTimeFrom = calendar.getTime();//再返回时间对象
        calendar.add(Calendar.DAY_OF_MONTH, 1);//把日期往后增加一天,整数  往后推,负数往前移动
        Date updateTimeTo = calendar.getTime();//返回另一个时间对象
        wrapper.between(ColumnConstant.UPDATE_TIME, updateTimeFrom, updateTimeTo);
        return getOne(wrapper);
    }

    @Override
    public List<CalibrationRecordDailyDto> searchCalibrationRecordDaily(
            Long calibrationId, Long resultNum
    ){
        QueryWrapper<CalibrationRecordDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("calibration_id", calibrationId);
        Calendar calendar = new GregorianCalendar();
//        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//当天时间赋给日历对象
        calendar.set(Calendar.HOUR_OF_DAY, 0);//设置对象日历格式
        calendar.set(Calendar.MINUTE, 0);//设置为当天零点
        calendar.set(Calendar.SECOND, 0);
        Date updateTimeFrom = calendar.getTime();//再返回时间对象
        calendar.add(Calendar.DAY_OF_MONTH, 1);//把日期往后增加一天,整数  往后推,负数往前移动
        Date updateTimeTo = calendar.getTime();//返回另一个时间对象
        wrapper.between(ColumnConstant.UPDATE_TIME, updateTimeFrom, updateTimeTo);
        wrapper.last("limit "+resultNum);
        wrapper.orderByDesc("completed_count");
        List<CalibrationRecordDaily> calibrationRecordDailyList = list(wrapper);
        return BeanCopyUtils.copyProperties(calibrationRecordDailyList, CalibrationRecordDailyDto.class);
    }

    @Override
    public List<CalibrationRecordDailyDto> searchCalibrationRecordAll(Long calibrationId,Long resultNum
    ){
        QueryWrapper<CalibrationRecordDaily> wrapper = new QueryWrapper<>();
        wrapper.select("calibration_id", "finisher", "calibration_detail_id", "sum(completed_count) as completedCount");
        wrapper.eq("calibration_id", calibrationId);
        wrapper.groupBy("finisher");
        wrapper.last("limit "+resultNum);
        wrapper.orderByDesc("completed_count");
        List<CalibrationRecordDaily> calibrationRecordDailyList = list(wrapper);
        return BeanCopyUtils.copyProperties(calibrationRecordDailyList, CalibrationRecordDailyDto.class);
    }

    @Override
    public List<CalibrationRecordDailyDto> searchCalibrationRecord(
            Long calibrationDetailId
    ){
        QueryWrapper<CalibrationRecordDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("calibration_detail_id", calibrationDetailId);
        Calendar calendar = new GregorianCalendar();
//        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//当天时间赋给日历对象
        calendar.set(Calendar.HOUR_OF_DAY, 0);//设置对象日历格式
        calendar.set(Calendar.MINUTE, 0);//设置为当天零点
        calendar.set(Calendar.SECOND, 0);
//        Date updateTimeFrom = calendar.getTime();//再返回时间对象
        calendar.add(Calendar.DAY_OF_MONTH, 1);//把日期往后增加一天,整数  往后推,负数往前移动
        Date updateTimeTo = calendar.getTime();//返回另一个时间对象
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date updateTimeFrom = calendar.getTime();
        wrapper.between(ColumnConstant.UPDATE_TIME, updateTimeFrom, updateTimeTo);
        wrapper.orderByDesc(ColumnConstant.UPDATE_TIME);
        List<CalibrationRecordDaily> calibrationRecordDailyList = list(wrapper);
        return BeanCopyUtils.copyProperties(calibrationRecordDailyList, CalibrationRecordDailyDto.class);
    }

}
